package kingiis.utils.simpleapp.view.refreshview;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import kingiis.utils.simpleapp.R;


/**
 * Created by King on 2017/12/29 0029.
 */

public class RefreshExtra {

    public static void add(RefreshLayout refresh, final Context context) {
        refresh.setHeaderView(new IHeaderView() {

            View view;
            TextView txt;
            //            View arr;
            String pullDownStr = "下拉刷新";
            String releaseRefreshStr = "释放刷新";
            String refreshingStr = "正在加载...";
            int GONE = View.GONE;
            int VISIBLE = View.VISIBLE;

            @Override
            public View getView() {
                view = View.inflate(context, R.layout.item_head_refresh, null);
                ImageView img = (ImageView) view.findViewById(R.id.head_iv);
                txt = (TextView) view.findViewById(R.id.head_text);
                try {
                    Glide.with(context).load(R.drawable.logoloading)
                            .asGif()
                            .dontAnimate()
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .into(img);
                } catch (Exception e) {

                }


                return view;
            }

            @Override
            public void onPullingDown(float fraction, float maxHeadHeight, float headHeight) {
                view.setVisibility(View.VISIBLE);
                if (fraction < 1f) {
                    txt.setText(pullDownStr);
                }
                if (fraction > 1f) {
                    txt.setText(releaseRefreshStr);
                }
            }

            @Override
            public void onPullReleasing(float fraction, float maxHeadHeight, float headHeight) {
                if (fraction < 1f) {
                    txt.setText(pullDownStr);
                }
            }

            @Override
            public void startAnim(float maxHeadHeight, float headHeight) {
                txt.setText(refreshingStr);
                view.setVisibility(VISIBLE);
            }

            @Override
            public void onFinish(OnAnimEndListener listener) {
                listener.onAnimEnd();
            }

            @Override
            public void reset() {

            }
        });
        refresh.setBottomView(new IBottomView() {
            View view;
            TextView txt;
            //            View arr;
            String pullDownStr = "上拉刷新";
            String releaseRefreshStr = "释放刷新";
            String refreshingStr = "正在加载...";
            int GONE = View.GONE;
            int VISIBLE = View.VISIBLE;

            @Override
            public View getView() {
                view = View.inflate(context, R.layout.item_bottom_refresh, null);
                ImageView img = (ImageView) view.findViewById(R.id.bottom_iv);
                try {
                    Glide.with(context).load(R.drawable.logoloading)
                            .asGif()
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .into(img);
                } catch (Exception e) {

                }
                txt = (TextView) view.findViewById(R.id.bottom_text);
                return view;
            }

            @Override
            public void onPullingUp(float fraction, float maxHeadHeight, float headHeight) {
                view.setVisibility(View.VISIBLE);
                if (fraction < 1f) {
                    txt.setText(pullDownStr);
                }
                if (fraction > 1f) {
                    txt.setText(releaseRefreshStr);
                }
            }

            @Override
            public void startAnim(float maxHeadHeight, float headHeight) {
                txt.setText(refreshingStr);
                view.setVisibility(VISIBLE);
            }

            @Override
            public void onPullReleasing(float fraction, float maxHeadHeight, float headHeight) {
                if (fraction < 1f) {
                    txt.setText(pullDownStr);
                }
            }

            @Override
            public void onFinish() {

            }

            @Override
            public void reset() {

            }
        });
    }

}

